package org.xxforest.game.notsing.screen.test;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.xxforest.game.core.XX;
import org.xxforest.game.core.scenes.scene2d.XXPointerTouch;
import org.xxforest.game.core.scenes.scene2d.XXScreen;
import org.xxforest.game.core.scenes.scene2d.events.XXEvent;
import org.xxforest.game.core.scenes.scene2d.events.XXEventListener;
import org.xxforest.game.core.scenes.scene2d.events.XXEventType;
import org.xxforest.game.core.scenes.scene2d.events.type.XXTouchDragEvent;
import org.xxforest.game.core.scenes.scene2d.graphics.XXParticle;
import org.xxforest.game.core.scenes.scene2d.selector.XXScheduleSelector;
import org.xxforest.game.core.tween.XXNodeAccessor;
import org.xxforest.game.core.tween.XXTween;

public class ParticleScreen extends XXScreen {

	private List<XXParticle> particles = new LinkedList<XXParticle>();
	
	@Override
	public void onCreate() {
		
		this.setListener(XXEventType.TOUCHDRAGGED, new XXEventListener() {
			
			@Override
			public boolean handle(XXEvent event) {
				XXParticle particle = XXParticle.create("a.p") ;
				
				XXTouchDragEvent dragEvent = (XXTouchDragEvent) event ;
				XXPointerTouch pointerTouch = dragEvent.getPointerTouch();
				particle.setPosition(pointerTouch.getToX(), pointerTouch.getToY());
				
				ParticleScreen.this.add(particle);
				particles.add(particle);
				
				XX.director.addTween(XXTween.to(particle, XXNodeAccessor.POS_XY, 2f).target(RandomUtils.nextFloat()*1800 - 900,RandomUtils.nextFloat()*1800 - 900));
				
				return false;
			}
		});
		
		XX.director.addSelector(new XXScheduleSelector() {
			
			@Override
			public void run() {
				for(XXParticle particle : particles){
					if(particle.isComplete()){
						remove(particle);
						particle.dispose() ;
					}
				}
			}
		}, 1);
		
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void resume() {
		
	}

	@Override
	public void show() {
		
	}

	@Override
	public void hide() {
		
	}

}
